/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Chat;

import java.awt.TextArea;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.List;
import java.util.Map;

/**
 *
 * @author snake_gt
 */
public class HiloServer implements Runnable {

    private Socket socket;
    private boolean bandera;
    private TextArea area;
    private Map lista;

    public HiloServer(Socket s, TextArea a,Map lista) {
        socket = s;
        area = a;
        this.lista = lista;
    }

    public void run() {
        bandera = true;
        while (bandera) {
            try {
                DataInputStream in = new DataInputStream(socket.getInputStream());
                String cad = in.readUTF();
                area.setText(area.getText() + cad + "\n");
               String arr[]=cad.split("#");
              Socket s=(Socket) lista.get(arr[0]);
              if(s!=null){
                    DataOutputStream out = new DataOutputStream(s.getOutputStream());
                    out.writeUTF(cad);
                }
            } catch (IOException ex) {
                bandera = false;
            }

        }
    }

    public void stop() {
        bandera = false;
    }
}
